package tester;

import tester.anticheeting.Matcher;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: aleck
 * Date: 2007-9-21
 * Time: 19:43:43
 * To change this template use File | Settings | File Templates.
 */
public class FileUtils {
  private FileUtils() {
  }

  public static boolean fileExists(String fileName) {
    File file = new File(fileName);
    return file.exists();
  }

  public static void deleteFile(String fileName) {
    File file = new File(fileName);
    if (file.exists())
      file.delete();
  }

  public static boolean createFile(String fileName) throws IOException {
    File file = new File(fileName);
    return file.createNewFile();
  }

  public static String fileToHtmlString(String fileName) {
    return fileToHtmlString(fileName, "Courier New");
  }

  public static String fileToHtmlString(String fileName, String fontName) {
    if (fileName == null)
      return "";
    try {
      byte[] byteArray = new byte[2];
      ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
      InputStreamReader isr = new InputStreamReader(bais);
      BufferedReader br = new BufferedReader(isr);
      StringBuilder sb = new StringBuilder(1024);
      sb.append("<html><font face=\"" + fontName + "\">");
      sb.append("<p>");
      FileInputStream fis = new FileInputStream(fileName);

      while (fis.available() > 0) {
        int i = fis.read();
        char c = (char)i;
        if (c == '\n') {
          sb.append("</p><p>");
        } else if (c == '\t')
          sb.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        else if (c == ' ')
          sb.append("&nbsp;");
        else if (c == '\"')
          sb.append("&quot;");
        else if (c == '&')
          sb.append("&amp;");
        else if (c == '<')
          sb.append("&lt;");
        else if (c == '>')
          sb.append("&gt;");
        else if (i > 127) {
          byteArray[0] = (byte)i;
          byteArray[1] = (byte)fis.read();
          bais.reset();
          sb.append((char)br.read());
        }
        else if (c != '\r')
          sb.append(c);
      }
      br.close();
      fis.close();
      sb.append("</p>");
      sb.append("</font></html>");
      return sb.toString();
    } catch (IOException e) {
      return "<html><font color=red>" + fileName + " does NOT exist.</font></html>";
    }
  }

  public static String fileToCompactString(String fileName) {
    return fileToCompactString(fileName, Matcher.MAX_MATCH_LEN);
  }

  public static String fileToCompactString(String fileName, int maxLen) {
    if (fileName == null) {
      return "";
    } else {
      final int MAX_BUFFER_LEN = 1024;
      StringBuilder sb = new StringBuilder(MAX_BUFFER_LEN);
      int len = 0;
      char[] c = new char[MAX_BUFFER_LEN];
      try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        int readLen = br.read(c, 0, MAX_BUFFER_LEN);
        while (readLen >= 0) {
          for (int i = 0; i < readLen; i++)
            if (c[i] != ' ' && c[i] != '\t') {
              sb.append(c[i]);
              len++;
              if (len >= maxLen)
                break;
            }
          if (len >= maxLen)
            break;
          else
            readLen = br.read(c, 0, MAX_BUFFER_LEN);
        }
      } catch (FileNotFoundException e) {
        return "";
      } catch (IOException e) {
      }
      return sb.toString();
    }
  }

}
